import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class StudentFileWriter {

    public static void main(String[] args) {
    	try {
        	Scanner keyboard = new Scanner(System.in);
        	System.out.print("Enter filename: ");
        	String filename = keyboard.next();
        	
            PrintWriter writer = new PrintWriter(filename);
            Student s;
            do {
            	s = getStudent(keyboard);
            	if(s != null) {
                	writer.print(s.getStudentNumber() + " ");
                	writer.print(s.getFamilyName() + " ");
                	writer.print(s.getGivenNames() + "\n");
            	}
            } while (s != null);
            writer.close();
            
        	keyboard.close();
        	
        	System.out.println("\nFile written: " + filename);
    	} catch(IOException e) {
    		System.out.println("IO Exception:\n" + e.getMessage());
    	}
    }
    
    public static Student getStudent(Scanner s) {
    	
    	int studentNumber;
    	String familyName;
    	String givenNames;
    	
    	System.out.println("\nPlease enter the next student's information.");
    	System.out.println("When finished, enter x for the student number.");

        // *** COMPLETE THE CODE IN THE SPACE BELOW ***

 
    	return new Student(familyName, givenNames, studentNumber);
    }

}
